﻿
@{
    ViewBag.Title = "Doctor Index";
}

@Html.Partial("_UserPage")
<div class="row searchbar">
    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 bar">@Html.Partial("_SearchPatient")</div>
</div>
<div id="patient" class="row result">
    <div class="col-xs-3 col-sm-3 col-md-2 col-lg-2 sidebar">@Html.Partial("_PatientInfo")</div>
    <div class="col-xs-9 col-sm-9 col-md-10 col-lg-10 main">
        <div class="row col-top">
            <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">@Html.Partial("~/Views/Doctor/ListOfVisitForDoctor.cshtml")</div>
        </div>
        <div class="row col-bottom">
            <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">@Html.Partial("~/Views/Doctor/ListOfIndicationForDoctor.cshtml")</div>
        </div>
    </div>
</div>

@Html.Partial("_Message")
@Html.Partial("~/Views/Doctor/CreateIndicationForm.cshtml")
@Html.Partial("~/Views/Doctor/ViewIndicationForm.cshtml")
@Html.Partial("~/Views/Doctor/CreateVisitForm.cshtml")
@Html.Partial("~/Views/Doctor/UpdateVisitForm.cshtml")
@Html.Partial("_PrintVisitForm")

@section Scripts {
    <script>
        var userType = '@User.UserType';
    </script>
    @Scripts.Render("~/bundles/message")
    @Scripts.Render("~/Scripts/BodyController.js")
    @Scripts.Render("~/Scripts/ListHeightController.js")
    @Scripts.Render("~/bundles/knockout")
    @Scripts.Render("~/bundles/my_knockout")
    @Scripts.Render("~/Scripts/ViewModel/PatientVM_Doctor.js")
    @Scripts.Render("~/Scripts/ViewModel/IndicationCreateVM.js")
    @Scripts.Render("~/Scripts/ViewModel/IndicationViewVM.js")
    @Scripts.Render("~/Scripts/ViewModel/VisitCreateVM.js")
    @Scripts.Render("~/Scripts/ViewModel/VisitUpdateVM.js")
    @Scripts.Render("~/Scripts/jquery.printArea.js")
    @Scripts.Render("~/Scripts/ViewModel/VisitPrintVM.js")
    <script>
        $(function () {
            ko.applyBindings(patientVM_Doctor, document.getElementById("patient"));
            ko.applyBindings(indicationCreateVM, document.getElementById("createIndicationVM"));
            ko.applyBindings(indicationViewVM, document.getElementById("viewIndicationVM"));
            ko.applyBindings(visitCreateVM, document.getElementById("createVisitVM"));
            ko.applyBindings(visitUpdateVM, document.getElementById("updateVisitVM"));
            ko.applyBindings(visitPrintVM, document.getElementById("printVisitVM"));
            var indexController = {
                searchTerm: $("#searchTerm"),
                btnSearch: $("#btnSearch"),
                //enableBtnSearch: function (state) {
                //},
                enableSearch: function (state) {
                    var p = this;
                    if (state == true) {
                        this.btnSearch.on("click", function () {
                            var searchType;
                            if ($("#searchPatientID").prop("checked") == true) {
                                searchType = 1;
                            }
                            else {
                                searchType = 2;
                            }
                            //alert(JSON.stringify({ id: $("#searchTerm").prop("value"), type: searchType }));
                            patientVM_Doctor.loadPatientInfo(p.searchTerm.prop("value"), searchType, indexController);
                        });
                        this.searchTerm.on("keypress", function (event) {
                            if (event.which == 13) {//Enter
                                p.btnSearch.trigger("click");
                            }
                        });
                    }
                    else {
                        p.btnSearch.off("click");
                        p.searchTerm.off("keypress");
                    }
                },
                message: messageController,
                body: bodyController,
                createIndication: $("#createIndication"),
                enableBtnCreateIndication: function (state) {
                    var self = this;
                    if(state==true){
                        self.createIndication.on("click", function () {
                            indicationCreateVM.loadPatientInfo(indexController);
                            self.displayCreateIndicationForm(true);
                        });
                        self.createIndication.removeClass("disabled");
                    }
                    else{
                        self.createIndication.off("click");
                        self.createIndication.addClass("disabled");
                    }
                },
                listIndication: $("#listIndication"),
                createIndicationForm: $("#createIndicationForm"),
                displayCreateIndicationForm: function(state){
                    if (state == true) {
                        this.createIndicationForm.css({
                            "display": "block"
                        });
                        this.body.displayBodyScroll(false);
                    }
                    else {
                        this.createIndicationForm.css({
                            "display": "none"
                        });
                        this.body.displayBodyScroll(true);
                    }
                },
                btnCreateIndicationSave: $("#create-indication-save"),
                btnCreateIndicationCancel: $("#create-indication-cancel"),
                viewIndicationForm: $("#viewIndicationForm"),
                displayViewIndicationForm: function(state){
                    if (state == true) {
                        this.viewIndicationForm.css({
                            "display": "block"
                        });
                        this.body.displayBodyScroll(false);
                    }
                    else {
                        this.viewIndicationForm.css({
                            "display": "none"
                        });
                        this.body.displayBodyScroll(true);
                    }
                },
                enableListIndication: function (state) {
                    var self = this;
                    if (state == true) {
                        self.enableBtnCreateIndication(true);
                        self.enableBtnCreateIndicationCancel(true);
                        self.listIndication.on("click", "tr", function (event) {
                            indicationViewVM.loadIndication($(event.currentTarget).find("td").eq(0).text(), indexController);
                            self.displayViewIndicationForm(true);
                        });
                        $("#view-indication-close").on("click", function () {
                            self.displayViewIndicationForm(false);
                            indicationViewVM.cancelLoadRequest();
                        });
                    }
                    else {
                        self.enableBtnCreateIndication(false);
                        this.listIndication.off("click");
                        self.enableBtnCreateIndicationCancel(false);
                        $("#view-indication-close").off("click");
                    }
                },
                enableBtnCreateIndicationSave: function (state) {
                    if (state == true) {
                        this.btnCreateIndicationSave.on("click", function () {
                            indicationCreateVM.saveIndication(indexController);
                        });
                    }
                    else {
                        this.btnCreateIndicationSave.off("click");
                    }
                },
                enableBtnCreateIndicationCancel: function (state) {
                    var self = this;
                    if (state == true) {
                        self.btnCreateIndicationCancel.on("click", function () {
                            self.displayCreateIndicationForm(false);
                        });
                    }
                    else {
                        self.btnCreateIndicationCancel.off("click");
                    }
                },

                btnFirstIndicationPage: $("#firstIndicationPage"),
                btnPreviousIndicationPage: $("#previousIndicationPage"),
                enableIndicationPreviousGroup: function (state) {
                    var self = this;
                    if (state == true) {
                        self.btnFirstIndicationPage.on("click", function () {
                            //patientVM_Doctor.IndicationsPage(1);
                            patientVM_Doctor.loadIndicationsPage(indexController, 1);
                        });
                        self.btnFirstIndicationPage.removeClass("disabled");
                        self.btnPreviousIndicationPage.on("click", function () {
                            //patientVM_Doctor.IndicationsPage(patientVM_Doctor.IndicationsPage()-1);
                            patientVM_Doctor.loadIndicationsPage(indexController, patientVM_Doctor.IndicationsPage() - 1);
                        });
                        self.btnPreviousIndicationPage.removeClass("disabled");
                    }
                    else {
                        self.btnFirstIndicationPage.off("click");
                        self.btnFirstIndicationPage.addClass("disabled");
                        self.btnPreviousIndicationPage.off("click");
                        self.btnPreviousIndicationPage.addClass("disabled");
                    }
                },

                btnLastIndicationPage: $("#lastIndicationPage"),
                btnNextIndicationPage: $("#nextIndicationPage"),
                enableIndicationNextGroup: function (state) {
                    var self = this;
                    if (state == true) {
                        self.btnLastIndicationPage.on("click", function () {
                            //patientVM_Doctor.IndicationsPage(patientVM_Doctor.IndicationsPageCount());
                            patientVM_Doctor.loadIndicationsPage(indexController, patientVM_Doctor.IndicationsPageCount());
                        });
                        self.btnLastIndicationPage.removeClass("disabled");
                        self.btnNextIndicationPage.on("click", function () {
                            //patientVM_Doctor.IndicationsPage(patientVM_Doctor.IndicationsPage()+1);
                            patientVM_Doctor.loadIndicationsPage(indexController, patientVM_Doctor.IndicationsPage() + 1);
                        });
                        self.btnNextIndicationPage.removeClass("disabled");
                    }
                    else {
                        self.btnLastIndicationPage.off("click");
                        self.btnLastIndicationPage.addClass("disabled");
                        self.btnNextIndicationPage.off("click");
                        self.btnNextIndicationPage.addClass("disabled");
                    }
                },

                //Visit
                createVisit: $("#createVisit"),
                listVisit: $("#listVisit"),

                createVisitForm: $("#createVisitForm"),
                displayCreateVisitForm: function(state){
                    if (state == true) {
                        this.createVisitForm.css({
                            "display": "block"
                        });
                        this.body.displayBodyScroll(false);
                    }
                    else {
                        this.createVisitForm.css({
                            "display": "none"
                        });
                        this.body.displayBodyScroll(true);
                    }
                },

                btnCreateVisitSave: $("#create-visit-save"),
                enableBtnCreateVisitSave: function(state){
                    if(state==true){
                        this.btnCreateVisitSave.on("click", function(){
                            visitCreateVM.saveVisit(indexController);
                        });
                    }
                    else{
                        this.btnCreateVisitSave.off("click");
                    }
                },

                btnCreateVisitCancel: $("#create-visit-cancel"),
                enableBtnCreateVisitCancel: function (state) {
                    var self = this;
                    if (state == true) {
                        self.btnCreateVisitCancel.on("click", function () {
                            self.displayCreateVisitForm(false);
                        });
                    }
                    else {
                        self.btnCreateVisitCancel.off("click");
                    }
                },

                btnUpdateVisitSave: $("#update-visit-save"),
                enableBtnUpdateVisitSave: function (state) {
                    if (state == true) {
                        this.btnUpdateVisitSave.on("click", function () {
                            visitUpdateVM.saveVisit(indexController);
                        });
                    }
                    else {
                        this.btnUpdateVisitSave.off("click");
                    }
                },

                btnUpdateVisitCancel: $("#update-visit-cancel"),
                enableBtnUpdateVisitCancel: function (state) {
                    var self = this;
                    if (state == true) {
                        self.btnUpdateVisitCancel.on("click", function () {
                            self.displayUpdateVisitForm(false);
                            visitUpdateVM.cancelLoadRequest();
                        });
                    }
                    else {
                        self.btnUpdateVisitCancel.off("click");
                    }
                },

                updateVisitForm: $("#updateVisitForm"),
                displayUpdateVisitForm: function (state) {
                    if (state == true) {
                        this.updateVisitForm.css({
                            "display": "block"
                        });
                        this.body.displayBodyScroll(false);
                    }
                    else {
                        this.updateVisitForm.css({
                            "display": "none"
                        });
                        this.body.displayBodyScroll(true);
                    }
                },

                printVisitForm: $("#printVisitForm"),
                displayPrintVisitForm: function (state) {
                    if (state == true) {
                        this.printVisitForm.css({
                            "display": "block"
                        });
                        this.body.displayBodyScroll(false);
                    }
                    else {
                        this.printVisitForm.css({
                            "display": "none"
                        });
                        this.body.displayBodyScroll(true);
                    }
                },

                printerLoading: function () {
                    indexController.message.processing("printer_loading");
                    indexController.message.processComplete("printer_wait");
                },
                printVisit: function () {
                    $("#printContent").printArea();
                },

                btnPrintVisitPrint: $("#print-visit-print"),
                enableBtnPrintVisitPrint: function (state) {
                    var self = this;
                    if (state == true) {
                        self.btnPrintVisitPrint.on("click", function () {
                            self.printVisit();
                            self.printerLoading();
                        });
                    }
                    else {
                        this.btnPrintVisitPrint.off("click");
                    }
                },

                btnPrintVisitClose: $("#print-visit-close"),
                enableBtnPrintVisitClose: function (state) {
                    var self = this;
                    if (state == true) {
                        self.btnPrintVisitClose.on("click", function () {
                            self.displayPrintVisitForm(false);
                            visitPrintVM.cancelLoadRequest();
                        });
                    }
                    else {
                        this.btnPrintVisitClose.off("click");
                    }
                },

                btnFirstVisitPage: $("#firstVisitPage"),
                btnPreviousVisitPage: $("#previousVisitPage"),
                enableVisitPreviousGroup: function (state) {
                    var self = this;
                    if(state==true){
                        self.btnFirstVisitPage.on("click", function () {
                            //patientVM_Doctor.VisitsPage(1);
                            patientVM_Doctor.loadVisitsPage(indexController, 1);
                        });
                        self.btnFirstVisitPage.removeClass("disabled");
                        self.btnPreviousVisitPage.on("click", function () {
                            //patientVM_Doctor.VisitsPage(patientVM_Doctor.VisitsPage()-1);
                            patientVM_Doctor.loadVisitsPage(indexController, patientVM_Doctor.VisitsPage() - 1);
                        });
                        self.btnPreviousVisitPage.removeClass("disabled");
                    }
                    else {
                        self.btnFirstVisitPage.off("click");
                        self.btnFirstVisitPage.addClass("disabled");
                        self.btnPreviousVisitPage.off("click");
                        self.btnPreviousVisitPage.addClass("disabled");
                    }
                },

                btnLastVisitPage: $("#lastVisitPage"),
                btnNextVisitPage: $("#nextVisitPage"),
                enableVisitNextGroup: function (state) {
                    var self = this;
                    if (state == true) {
                        self.btnLastVisitPage.on("click", function () {
                            //patientVM_Doctor.VisitsPage(patientVM_Doctor.VisitsPageCount());
                            patientVM_Doctor.loadVisitsPage(indexController, patientVM_Doctor.VisitsPageCount());
                        });
                        self.btnLastVisitPage.removeClass("disabled");
                        self.btnNextVisitPage.on("click", function () {
                            //patientVM_Doctor.VisitsPage(patientVM_Doctor.VisitsPage()+1);
                            patientVM_Doctor.loadVisitsPage(indexController, patientVM_Doctor.VisitsPage() + 1);
                        });
                        self.btnNextVisitPage.removeClass("disabled");
                    }
                    else {
                        self.btnLastVisitPage.off("click");
                        self.btnLastVisitPage.addClass("disabled");
                        self.btnNextVisitPage.off("click");
                        self.btnNextVisitPage.addClass("disabled");
                    }
                },

                enableBtnCreateVisit: function (state) {
                    var self = this;
                    if (state == true) {
                        self.createVisit.on("click", function () {
                            visitCreateVM.loadVisit(indexController);
                            self.displayCreateVisitForm(true);
                        });
                        self.createVisit.removeClass("disabled");
                    }
                    else {
                        this.createVisit.off("click");
                        self.createVisit.addClass("disabled");
                    }
                },

                enableListVisit: function (state) {
                    var self = this;
                    if (state == true) {
                        self.enableBtnCreateVisit(true);
                        self.enableBtnCreateVisitCancel(true);
                        self.listVisit.on("click", "tr", function (event) {
                            visitUpdateVM.loadVisit($(event.currentTarget).find("td").eq(0).text(), indexController);
                            self.displayUpdateVisitForm(true);
                        });
                        self.enableBtnUpdateVisitCancel(true);

                        self.listVisit.on("click", ".print", function (event) {
                            visitPrintVM.loadVisit($(event.currentTarget).parent("tr").find("td").eq(0).text(), indexController);
                            self.displayPrintVisitForm(true);
                            event.stopPropagation();
                        });
                        self.enableBtnPrintVisitClose(true);
                        //self.enablePreviousGroup(true);
                        //self.enableNextGroup(true);
                    }
                    else {
                        self.enableBtnCreateVisit(false);
                        self.enableBtnCreateVisitCancel(false);

                        this.listVisit.off("click");
                        self.enableBtnUpdateVisitCancel(false);

                        self.enableBtnPrintVisitClose(false);

                        //self.enablePreviousGroup(false);
                        //self.enableNextGroup(false);
                    }
                },
            };
            //indexController.enableBtnSearch(true);
            indexController.enableSearch(true);
            indexController.searchTerm.focus();
            indexController.enableBtnCreateVisit(false);
            indexController.enableBtnCreateIndication(false);
            indexController.enableIndicationPreviousGroup(false);
            indexController.enableIndicationNextGroup(false);
            indexController.enableVisitPreviousGroup(false);
            indexController.enableVisitNextGroup(false);
        });
    </script>
}